<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CreateActionLogTable extends AbstractMigration
{
    public function change(): void
    {
        $table = $this->table('action_log', [
            'id' => true,
            'primary_key' => ['id'],
            'comment' => '行为日志表',
            'engine' => 'InnoDB',
            'encoding' => 'utf8mb4',
        ]);
        $table->addColumn('admin_id', 'integer', [
            'null' => false,
            'signed' => false,
            'comment' => '管理员ID',
        ])->addColumn('admin_name', 'string', [
            'limit' => 20,
            'null' => false,
            'default' => '',
            'comment' => '管理员名称',
        ])->addColumn('url', 'text', [
            'null' => false,
            'comment' => '请求URL',
        ])->addColumn('ip', 'string', [
            'limit' => 45,
            'null' => false,
            'default' => '',
            'comment' => 'IP地址',
        ])->addColumn('user_agent', 'text', [
            'null' => true,
            'comment' => 'User Agent',
        ])->addColumn('request_data', 'text', [
            'null' => true,
            'comment' => '请求数据',
        ])->addColumn('response_data', 'text', [
            'null' => true,
            'comment' => '响应数据',
        ])->addColumn('execution_time', 'integer', [
            'null' => true,
            'signed' => false,
            'comment' => '执行时间(毫秒)',
        ])->addColumn('status', 'boolean', [
            'null' => false,
            'default' => 1,
            'comment' => '状态:0=失败,1=成功',
        ])->addColumn('create_time', 'timestamp', [
            'default' => 'CURRENT_TIMESTAMP',
            'comment' => '创建时间',
        ])->addIndex(['admin_id'], [
            'name' => 'idx_admin_id',
        ])->addIndex(['status'], [
            'name' => 'idx_status',
        ])->addIndex(['create_time'], [
            'name' => 'idx_create_time',
        ])->create();
    }
}